package com.qd.panda.service.stroke;

import cn.hutool.core.math.Money;
import com.qd.common.panda.domain.entity.carpark.calculate.CalculateDTO;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

/**
 * @author sjk
 */
@Slf4j
public class BigDecimalUtil {
    private BigDecimalUtil() {

    }

    /**
     * 默认精确到小数点后两位
     *
     * @param bigDecimal 传入的数字
     * @return 四舍五入后的数字
     */
    public static BigDecimal buildDefaultScale(BigDecimal bigDecimal) {
        return bigDecimal.setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 默认精确到小数点后两位
     *
     * @param bigDecimal 传入的数字
     * @param scale      精确到几位
     * @return 四舍五入后的数字
     */
    public static BigDecimal buildDefaultScale(BigDecimal bigDecimal, int scale) {
        return bigDecimal.setScale(scale, RoundingMode.HALF_UP);
    }

    /**
     * 计算百分比，取整
     *
     * @param num1 第一个数字
     * @param num2 第二个数字
     * @return 百分比
     */
    public static int percentage(Integer num1, Integer num2) {
        NumberFormat numberformat = java.text.NumberFormat.getInstance();
        numberformat.setMaximumFractionDigits(0);
        String result = numberformat.format(((double) num1 / (double) num2) * 100);
        return Integer.parseInt(result);
    }

    /**
     * 计算折扣价格
     *
     * @param originalPrice    原价
     * @param customerDiscount 折扣
     * @return 折后价格
     */
    public static CalculateDTO calculatePrice(BigDecimal originalPrice, Double customerDiscount) {
        CalculateDTO calculateDTO = new CalculateDTO();
        calculateDTO.setPrice(originalPrice);
        if (null != customerDiscount) {
            Money money = new Money(originalPrice);
            final Money multiply = money.multiply(customerDiscount);
            final Money divide = multiply.divide(100);
            final BigDecimal amount = divide.getAmount();
            calculateDTO.setCalculatePrice(amount);
            final Money subtract = money.subtract(divide);
            calculateDTO.setDiscountAmount(subtract.getAmount());
        } else {
            calculateDTO.setCalculatePrice(calculateDTO.getPrice());
            calculateDTO.setDiscountAmount(new BigDecimal(0));
        }
        return calculateDTO;
    }

    /**
     * 计算折扣
     *
     * @param originalPrice 原价
     * @param customerPrice 优惠价
     * @return 折后
     */
    public static CalculateDTO calculateDiscount(BigDecimal originalPrice, BigDecimal customerPrice) {
        CalculateDTO calculateDTO = new CalculateDTO();
        calculateDTO.setPrice(originalPrice);
        calculateDTO.setCalculatePrice(customerPrice);
        if (null != customerPrice) {
            BigDecimal divide = customerPrice.divide(originalPrice, 4, RoundingMode.HALF_UP);
            BigDecimal multiply = divide.multiply(new BigDecimal("100"));
            calculateDTO.setCustomerDiscount(buildDefaultScale(multiply));
            if (customerPrice.compareTo(BigDecimal.ZERO) == BigDecimal.ONE.signum()) {
                calculateDTO.setDiscountAmount(originalPrice.remainder(customerPrice));
            }
        } else {
            calculateDTO.setCalculatePrice(calculateDTO.getPrice());
            calculateDTO.setDiscountAmount(new BigDecimal(0));
        }
        return calculateDTO;
    }

}